﻿using MyCommunity.Models;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

public class UserRegisterModel
{
    private User obj;

    public UserRegisterModel() {
        this.obj = new User();
    }

    public UserRegisterModel(User obj)
    {
        this.Id = obj.Id;
        this.Firstname = obj.Firstname;
        this.Lastname = obj.Lastname;
        this.Mail = obj.Mail;
        this.Password = obj.Password;
    }

    public int Id { get; private set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$",
    ErrorMessage = "Invalid email address.")]
    public string Mail { get; set; }

    [Required]
    public string Firstname { get; set; }

    [Required]
    public string Lastname { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public void ApplyChanges(User obj)
    {
        obj.Firstname = this.Firstname;
        obj.Lastname = this.Lastname;
        obj.Mail = this.Mail;
        obj.Password = UserService.MD5Hash(this.Password);
    }
}